home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / ds3100.md / gdb / RCS / utils.c,v < prev    next >
Encoding:
Text File  |  1992-07-01  |  32.5 KB  |  1,405 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     92.06.17.11.48.45;  author secor;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* General utility routines for GDB, the GNU debugger.
  26.    Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  27.  
  28. This file is part of GDB.
  29.  
  30. This program is free software; you can redistribute it and/or modify
  31. it under the terms of the GNU General Public License as published by
  32. the Free Software Foundation; either version 2 of the License, or
  33. (at your option) any later version.
  34.  
  35. This program is distributed in the hope that it will be useful,
  36. but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38. GNU General Public License for more details.
  39.  
  40. You should have received a copy of the GNU General Public License
  41. along with this program; if not, write to the Free Software
  42. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  43.  
  44. #include "defs.h"
  45.  
  46. #include <sys/ioctl.h>
  47. #include <sys/param.h>
  48. #include <pwd.h>
  49. #include <varargs.h>
  50. #include <ctype.h>
  51. #include <string.h>
  52.  
  53. #include "signals.h"
  54. #include "gdbcmd.h"
  55. #include "terminal.h"
  56. #include "bfd.h"
  57. #include "target.h"
  58.  
  59. /* Prototypes for local functions */
  60.  
  61. #if !defined (NO_MALLOC_CHECK)
  62.  
  63. static void
  64. malloc_botch PARAMS ((void));
  65.  
  66. #endif /* NO_MALLOC_CHECK  */
  67.  
  68. static void
  69. fatal_dump_core ();    /* Can't prototype with <varargs.h> usage... */
  70.  
  71. static void
  72. prompt_for_continue PARAMS ((void));
  73.  
  74. static void 
  75. set_width_command PARAMS ((char *, int, struct cmd_list_element *));
  76.  
  77. static void
  78. vfprintf_filtered PARAMS ((FILE *, char *, va_list));
  79.  
  80. /* If this definition isn't overridden by the header files, assume
  81.    that isatty and fileno exist on this system.  */
  82. #ifndef ISATTY
  83. #define ISATTY(FP)    (isatty (fileno (FP)))
  84. #endif
  85.  
  86. /* Chain of cleanup actions established with make_cleanup,
  87.    to be executed if an error happens.  */
  88.  
  89. static struct cleanup *cleanup_chain;
  90.  
  91. /* Nonzero means a quit has been requested.  */
  92.  
  93. int quit_flag;
  94.  
  95. /* Nonzero means quit immediately if Control-C is typed now,
  96.    rather than waiting until QUIT is executed.  */
  97.  
  98. int immediate_quit;
  99.  
  100. /* Nonzero means that encoded C++ names should be printed out in their
  101.    C++ form rather than raw.  */
  102.  
  103. int demangle = 1;
  104.  
  105. /* Nonzero means that encoded C++ names should be printed out in their
  106.    C++ form even in assembler language displays.  If this is set, but
  107.    DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls.  */
  108.  
  109. int asm_demangle = 0;
  110.  
  111. /* Nonzero means that strings with character values >0x7F should be printed
  112.    as octal escapes.  Zero means just print the value (e.g. it's an
  113.    international character, and the terminal or window can cope.)  */
  114.  
  115. int sevenbit_strings = 0;
  116.  
  117. #ifdef sprite
  118. /*
  119.  * Cached value for ISATTY((stdout)). This value is cached on Sprite
  120.  * because the isatty() ioctl requires a pdev exchange with the tty driver
  121.  * taking at least 2 ms.  This makes output very slow on gdb.
  122.  */
  123. int isatty_stdout;
  124. #endif
  125.  
  126. /* String to be printed before error messages, if any.  */
  127.  
  128. char *error_pre_print;
  129. char *warning_pre_print = "\nwarning: ";
  130.  
  131. /* Add a new cleanup to the cleanup_chain,
  132.    and return the previous chain pointer
  133.    to be passed later to do_cleanups or discard_cleanups.
  134.    Args are FUNCTION to clean up with, and ARG to pass to it.  */
  135.  
  136. struct cleanup *
  137. make_cleanup (function, arg)
  138.      void (*function) PARAMS ((PTR));
  139.      PTR arg;
  140. {
  141.   register struct cleanup *new
  142.     = (struct cleanup *) xmalloc (sizeof (struct cleanup));
  143.   register struct cleanup *old_chain = cleanup_chain;
  144.  
  145.   new->next = cleanup_chain;
  146.   new->function = function;
  147.   new->arg = arg;
  148.   cleanup_chain = new;
  149.  
  150.   return old_chain;
  151. }
  152.  
  153. /* Discard cleanups and do the actions they describe
  154.    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
  155.  
  156. void
  157. do_cleanups (old_chain)
  158.      register struct cleanup *old_chain;
  159. {
  160.   register struct cleanup *ptr;
  161.   while ((ptr = cleanup_chain) != old_chain)
  162.     {
  163.       cleanup_chain = ptr->next;    /* Do this first incase recursion */
  164.       (*ptr->function) (ptr->arg);
  165.       free (ptr);
  166.     }
  167. }
  168.  
  169. /* Discard cleanups, not doing the actions they describe,
  170.    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
  171.  
  172. void
  173. discard_cleanups (old_chain)
  174.      register struct cleanup *old_chain;
  175. {
  176.   register struct cleanup *ptr;
  177.   while ((ptr = cleanup_chain) != old_chain)
  178.     {
  179.       cleanup_chain = ptr->next;
  180.       free ((PTR)ptr);
  181.     }
  182. }
  183.  
  184. /* Set the cleanup_chain to 0, and return the old cleanup chain.  */
  185. struct cleanup *
  186. save_cleanups ()
  187. {
  188.   struct cleanup *old_chain = cleanup_chain;
  189.  
  190.   cleanup_chain = 0;
  191.   return old_chain;
  192. }
  193.  
  194. /* Restore the cleanup chain from a previously saved chain.  */
  195. void
  196. restore_cleanups (chain)
  197.      struct cleanup *chain;
  198. {
  199.   cleanup_chain = chain;
  200. }
  201.  
  202. /* This function is useful for cleanups.
  203.    Do
  204.  
  205.      foo = xmalloc (...);
  206.      old_chain = make_cleanup (free_current_contents, &foo);
  207.  
  208.    to arrange to free the object thus allocated.  */
  209.  
  210. void
  211. free_current_contents (location)
  212.      char **location;
  213. {
  214.   free (*location);
  215. }
  216.  
  217. /* Provide a known function that does nothing, to use as a base for
  218.    for a possibly long chain of cleanups.  This is useful where we
  219.    use the cleanup chain for handling normal cleanups as well as dealing
  220.    with cleanups that need to be done as a result of a call to error().
  221.    In such cases, we may not be certain where the first cleanup is, unless
  222.    we have a do-nothing one to always use as the base. */
  223.  
  224. /* ARGSUSED */
  225. void
  226. null_cleanup (arg)
  227.     char **arg;
  228. {
  229. }
  230.  
  231.  
  232. /* Provide a hook for modules wishing to print their own warning messages
  233.    to set up the terminal state in a compatible way, without them having
  234.    to import all the target_<...> macros. */
  235.  
  236. void
  237. warning_setup ()
  238. {
  239.   target_terminal_ours ();
  240.   wrap_here("");            /* Force out any buffered output */
  241.   fflush (stdout);
  242. }
  243.  
  244. /* Print a warning message.
  245.    The first argument STRING is the warning message, used as a fprintf string,
  246.    and the remaining args are passed as arguments to it.
  247.    The primary difference between warnings and errors is that a warning
  248.    does not force the return to command level. */
  249.  
  250. /* VARARGS */
  251. void
  252. warning (va_alist)
  253.      va_dcl
  254. {
  255.   va_list args;
  256.   char *string;
  257.  
  258.   va_start (args);
  259.   target_terminal_ours ();
  260.   wrap_here("");            /* Force out any buffered output */
  261.   fflush (stdout);
  262.   if (warning_pre_print)
  263.     fprintf (stderr, warning_pre_print);
  264.   string = va_arg (args, char *);
  265.   vfprintf (stderr, string, args);
  266.   fprintf (stderr, "\n");
  267.   va_end (args);
  268. }
  269.  
  270. /* Print an error message and return to command level.
  271.    The first argument STRING is the error message, used as a fprintf string,
  272.    and the remaining args are passed as arguments to it.  */
  273.  
  274. /* VARARGS */
  275. NORETURN void
  276. error (va_alist)
  277.      va_dcl
  278. {
  279.   va_list args;
  280.   char *string;
  281.  
  282.   va_start (args);
  283.   target_terminal_ours ();
  284.   wrap_here("");            /* Force out any buffered output */
  285.   fflush (stdout);
  286.   if (error_pre_print)
  287.     fprintf (stderr, error_pre_print);
  288.   string = va_arg (args, char *);
  289.   vfprintf (stderr, string, args);
  290.   fprintf (stderr, "\n");
  291.   va_end (args);
  292.   return_to_top_level ();
  293. }
  294.  
  295. /* Print an error message and exit reporting failure.
  296.    This is for a error that we cannot continue from.
  297.    The arguments are printed a la printf.
  298.  
  299.    This function cannot be declared volatile (NORETURN) in an
  300.    ANSI environment because exit() is not declared volatile. */
  301.  
  302. /* VARARGS */
  303. NORETURN void
  304. fatal (va_alist)
  305.      va_dcl
  306. {
  307.   va_list args;
  308.   char *string;
  309.  
  310.   va_start (args);
  311.   string = va_arg (args, char *);
  312.   fprintf (stderr, "\ngdb: ");
  313.   vfprintf (stderr, string, args);
  314.   fprintf (stderr, "\n");
  315.   va_end (args);
  316.   exit (1);
  317. }
  318.  
  319. /* Print an error message and exit, dumping core.
  320.    The arguments are printed a la printf ().  */
  321.  
  322. /* VARARGS */
  323. static void
  324. fatal_dump_core (va_alist)
  325.      va_dcl
  326. {
  327.   va_list args;
  328.   char *string;
  329.  
  330.   va_start (args);
  331.   string = va_arg (args, char *);
  332.   /* "internal error" is always correct, since GDB should never dump
  333.      core, no matter what the input.  */
  334.   fprintf (stderr, "\ngdb internal error: ");
  335.   vfprintf (stderr, string, args);
  336.   fprintf (stderr, "\n");
  337.   va_end (args);
  338.  
  339.   signal (SIGQUIT, SIG_DFL);
  340.   kill (getpid (), SIGQUIT);
  341.   /* We should never get here, but just in case...  */
  342.   exit (1);
  343. }
  344.  
  345. /* Print the system error message for errno, and also mention STRING
  346.    as the file name for which the error was encountered.
  347.    Then return to command level.  */
  348.  
  349. void
  350. perror_with_name (string)
  351.      char *string;
  352. {
  353.   extern int sys_nerr;
  354.   extern char *sys_errlist[];
  355.   char *err;
  356.   char *combined;
  357.  
  358.   if (errno < sys_nerr)
  359.     err = sys_errlist[errno];
  360.   else
  361.     err = "unknown error";
  362.  
  363.   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  364.   strcpy (combined, string);
  365.   strcat (combined, ": ");
  366.   strcat (combined, err);
  367.  
  368.   /* I understand setting these is a matter of taste.  Still, some people
  369.      may clear errno but not know about bfd_error.  Doing this here is not
  370.      unreasonable. */
  371.   bfd_error = no_error;
  372.   errno = 0;
  373.  
  374.   error ("%s.", combined);
  375. }
  376.  
  377. /* Print the system error message for ERRCODE, and also mention STRING
  378.    as the file name for which the error was encountered.  */
  379.  
  380. void
  381. print_sys_errmsg (string, errcode)
  382.      char *string;
  383.      int errcode;
  384. {
  385.   extern int sys_nerr;
  386.   extern char *sys_errlist[];
  387.   char *err;
  388.   char *combined;
  389.  
  390.   if (errcode < sys_nerr)
  391.     err = sys_errlist[errcode];
  392.   else
  393.     err = "unknown error";
  394.  
  395.   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  396.   strcpy (combined, string);
  397.   strcat (combined, ": ");
  398.   strcat (combined, err);
  399.  
  400.   printf ("%s.\n", combined);
  401. }
  402.  
  403. /* Control C eventually causes this to be called, at a convenient time.  */
  404.  
  405. void
  406. quit ()
  407. {
  408.   target_terminal_ours ();
  409.   wrap_here ((char *)0);        /* Force out any pending output */
  410. #ifdef HAVE_TERMIO
  411.   ioctl (fileno (stdout), TCFLSH, 1);
  412. #else /* not HAVE_TERMIO */
  413.   ioctl (fileno (stdout), TIOCFLUSH, 0);
  414. #endif /* not HAVE_TERMIO */
  415. #ifdef TIOCGPGRP
  416.   error ("Quit");
  417. #else
  418.   error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
  419. #endif /* TIOCGPGRP */
  420. }
  421.  
  422. /* Control C comes here */
  423.  
  424. void
  425. request_quit (signo)
  426.      int signo;
  427. {
  428.   quit_flag = 1;
  429.  
  430. #ifdef USG
  431.   /* Restore the signal handler.  */
  432.   signal (signo, request_quit);
  433. #endif
  434.  
  435.   if (immediate_quit)
  436.     quit ();
  437. }
  438.  
  439.  
  440. /* Memory management stuff (malloc friends).  */
  441.  
  442. #if defined (NO_MMALLOC)
  443.  
  444. PTR
  445. mmalloc (md, size)
  446.      PTR md;
  447.      long size;
  448. {
  449.   return (malloc (size));
  450. }
  451.  
  452. PTR
  453. mrealloc (md, ptr, size)
  454.      PTR md;
  455.      PTR ptr;
  456.      long size;
  457. {
  458.   if (ptr == 0)        /* Guard against old realloc's */
  459.     return malloc (size);
  460.   else
  461.     return realloc (ptr, size);
  462. }
  463.  
  464. void
  465. mfree (md, ptr)
  466.      PTR md;
  467.      PTR ptr;
  468. {
  469.   free (ptr);
  470. }
  471.  
  472. #endif    /* NO_MMALLOC */
  473.  
  474. #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
  475.  
  476. void
  477. init_malloc (md)
  478.      PTR md;
  479. {
  480. }
  481.  
  482. #else /* have mmalloc and want corruption checking  */
  483.  
  484. static void
  485. malloc_botch ()
  486. {
  487.   fatal_dump_core ("Memory corruption");
  488. }
  489.  
  490. /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
  491.    by MD, to detect memory corruption.  Note that MD may be NULL to specify
  492.    the default heap that grows via sbrk.
  493.  
  494.    Note that for freshly created regions, we must call mmcheck prior to any
  495.    mallocs in the region.  Otherwise, any region which was allocated prior to
  496.    installing the checking hooks, which is later reallocated or freed, will
  497.    fail the checks!  The mmcheck function only allows initial hooks to be
  498.    installed before the first mmalloc.  However, anytime after we have called
  499.    mmcheck the first time to install the checking hooks, we can call it again
  500.    to update the function pointer to the memory corruption handler.
  501.  
  502.    Returns zero on failure, non-zero on success. */
  503.  
  504. void
  505. init_malloc (md)
  506.      PTR md;
  507. {
  508.   if (!mmcheck (md, malloc_botch))
  509.     {
  510.       warning ("internal error: failed to install memory consistency checks");
  511.     }
  512.  
  513.   (void) mmtrace ();
  514. }
  515.  
  516. #endif /* Have mmalloc and want corruption checking  */
  517.  
  518. /* Called when a memory allocation fails, with the number of bytes of
  519.    memory requested in SIZE. */
  520.  
  521. NORETURN void
  522. nomem (size)
  523.      long size;
  524. {
  525.   if (size > 0)
  526.     {
  527.       fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
  528.     }
  529.   else
  530.     {
  531.       fatal ("virtual memory exhausted.");
  532.     }
  533. }
  534.  
  535. /* Like mmalloc but get error if no storage available, and protect against
  536.    the caller wanting to allocate zero bytes.  Whether to return NULL for
  537.    a zero byte request, or translate the request into a request for one
  538.    byte of zero'd storage, is a religious issue. */
  539.  
  540. PTR
  541. xmmalloc (md, size)
  542.      PTR md;
  543.      long size;
  544. {
  545.   register PTR val;
  546.  
  547.   if (size == 0)
  548.     {
  549.       val = NULL;
  550.     }
  551.   else if ((val = mmalloc (md, size)) == NULL)
  552.     {
  553.       nomem (size);
  554.     }
  555.   return (val);
  556. }
  557.  
  558. /* Like mrealloc but get error if no storage available.  */
  559.  
  560. PTR
  561. xmrealloc (md, ptr, size)
  562.      PTR md;
  563.      PTR ptr;
  564.      long size;
  565. {
  566.   register PTR val;
  567.  
  568.   if (ptr != NULL)
  569.     {
  570.       val = mrealloc (md, ptr, size);
  571.     }
  572.   else
  573.     {
  574.       val = mmalloc (md, size);
  575.     }
  576.   if (val == NULL)
  577.     {
  578.       nomem (size);
  579.     }
  580.   return (val);
  581. }
  582.  
  583. /* Like malloc but get error if no storage available, and protect against
  584.    the caller wanting to allocate zero bytes.  */
  585.  
  586. PTR
  587. xmalloc (size)
  588.      long size;
  589. {
  590.   return (xmmalloc ((void *) NULL, size));
  591. }
  592.  
  593. /* Like mrealloc but get error if no storage available.  */
  594.  
  595. PTR
  596. xrealloc (ptr, size)
  597.      PTR ptr;
  598.      long size;
  599. {
  600.   return (xmrealloc ((void *) NULL, ptr, size));
  601. }
  602.  
  603.  
  604. /* My replacement for the read system call.
  605.    Used like `read' but keeps going if `read' returns too soon.  */
  606.  
  607. int
  608. myread (desc, addr, len)
  609.      int desc;
  610.      char *addr;
  611.      int len;
  612. {
  613.   register int val;
  614.   int orglen = len;
  615.  
  616.   while (len > 0)
  617.     {
  618.       val = read (desc, addr, len);
  619.       if (val < 0)
  620.     return val;
  621.       if (val == 0)
  622.     return orglen - len;
  623.       len -= val;
  624.       addr += val;
  625.     }
  626.   return orglen;
  627. }
  628.  
  629. /* Make a copy of the string at PTR with SIZE characters
  630.    (and add a null character at the end in the copy).
  631.    Uses malloc to get the space.  Returns the address of the copy.  */
  632.  
  633. char *
  634. savestring (ptr, size)
  635.      const char *ptr;
  636.      int size;
  637. {
  638.   register char *p = (char *) xmalloc (size + 1);
  639.   bcopy (ptr, p, size);
  640.   p[size] = 0;
  641.   return p;
  642. }
  643.  
  644. char *
  645. msavestring (md, ptr, size)
  646.      void *md;
  647.      const char *ptr;
  648.      int size;
  649. {
  650.   register char *p = (char *) xmmalloc (md, size + 1);
  651.   bcopy (ptr, p, size);
  652.   p[size] = 0;
  653.   return p;
  654. }
  655.  
  656. /* The "const" is so it compiles under DGUX (which prototypes strsave
  657.    in <string.h>.  FIXME: This should be named "xstrsave", shouldn't it?
  658.    Doesn't real strsave return NULL if out of memory?  */
  659. char *
  660. strsave (ptr)
  661.      const char *ptr;
  662. {
  663.   return savestring (ptr, strlen (ptr));
  664. }
  665.  
  666. char *
  667. mstrsave (md, ptr)
  668.      void *md;
  669.      const char *ptr;
  670. {
  671.   return (msavestring (md, ptr, strlen (ptr)));
  672. }
  673.  
  674. void
  675. print_spaces (n, file)
  676.      register int n;
  677.      register FILE *file;
  678. {
  679.   while (n-- > 0)
  680.     fputc (' ', file);
  681. }
  682.  
  683. /* Ask user a y-or-n question and return 1 iff answer is yes.
  684.    Takes three args which are given to printf to print the question.
  685.    The first, a control string, should end in "? ".
  686.    It should not say how to answer, because we do that.  */
  687.  
  688. /* VARARGS */
  689. int
  690. query (va_alist)
  691.      va_dcl
  692. {
  693.   va_list args;
  694.   char *ctlstr;
  695.   register int answer;
  696.   register int ans2;
  697.  
  698.   /* Automatically answer "yes" if input is not from a terminal.  */
  699.   if (!input_from_terminal_p ())
  700.     return 1;
  701.  
  702.   while (1)
  703.     {
  704.       va_start (args);
  705.       ctlstr = va_arg (args, char *);
  706.       vfprintf (stdout, ctlstr, args);
  707.       va_end (args);
  708.       printf ("(y or n) ");
  709.       fflush (stdout);
  710.       answer = fgetc (stdin);
  711.       clearerr (stdin);        /* in case of C-d */
  712.       if (answer == EOF)    /* C-d */
  713.         return 1;
  714.       if (answer != '\n')    /* Eat rest of input line, to EOF or newline */
  715.     do 
  716.       {
  717.         ans2 = fgetc (stdin);
  718.         clearerr (stdin);
  719.       }
  720.         while (ans2 != EOF && ans2 != '\n');
  721.       if (answer >= 'a')
  722.     answer -= 040;
  723.       if (answer == 'Y')
  724.     return 1;
  725.       if (answer == 'N')
  726.     return 0;
  727.       printf ("Please answer y or n.\n");
  728.     }
  729. }
  730.  
  731.  
  732. /* Parse a C escape sequence.  STRING_PTR points to a variable
  733.    containing a pointer to the string to parse.  That pointer
  734.    should point to the character after the \.  That pointer
  735.    is updated past the characters we use.  The value of the
  736.    escape sequence is returned.
  737.  
  738.    A negative value means the sequence \ newline was seen,
  739.    which is supposed to be equivalent to nothing at all.
  740.  
  741.    If \ is followed by a null character, we return a negative
  742.    value and leave the string pointer pointing at the null character.
  743.  
  744.    If \ is followed by 000, we return 0 and leave the string pointer
  745.    after the zeros.  A value of 0 does not mean end of string.  */
  746.  
  747. int
  748. parse_escape (string_ptr)
  749.      char **string_ptr;
  750. {
  751.   register int c = *(*string_ptr)++;
  752.   switch (c)
  753.     {
  754.     case 'a':
  755.       return 007;        /* Bell (alert) char */
  756.     case 'b':
  757.       return '\b';
  758.     case 'e':            /* Escape character */
  759.       return 033;
  760.     case 'f':
  761.       return '\f';
  762.     case 'n':
  763.       return '\n';
  764.     case 'r':
  765.       return '\r';
  766.     case 't':
  767.       return '\t';
  768.     case 'v':
  769.       return '\v';
  770.     case '\n':
  771.       return -2;
  772.     case 0:
  773.       (*string_ptr)--;
  774.       return 0;
  775.     case '^':
  776.       c = *(*string_ptr)++;
  777.       if (c == '\\')
  778.     c = parse_escape (string_ptr);
  779.       if (c == '?')
  780.     return 0177;
  781.       return (c & 0200) | (c & 037);
  782.       
  783.     case '0':
  784.     case '1':
  785.     case '2':
  786.     case '3':
  787.     case '4':
  788.     case '5':
  789.     case '6':
  790.     case '7':
  791.       {
  792.     register int i = c - '0';
  793.     register int count = 0;
  794.     while (++count < 3)
  795.       {
  796.         if ((c = *(*string_ptr)++) >= '0' && c <= '7')
  797.           {
  798.         i *= 8;
  799.         i += c - '0';
  800.           }
  801.         else
  802.           {
  803.         (*string_ptr)--;
  804.         break;
  805.           }
  806.       }
  807.     return i;
  808.       }
  809.     default:
  810.       return c;
  811.     }
  812. }
  813.  
  814. /* Print the character C on STREAM as part of the contents
  815.    of a literal string whose delimiter is QUOTER.  */
  816.  
  817. void
  818. printchar (c, stream, quoter)
  819.      register int c;
  820.      FILE *stream;
  821.      int quoter;
  822. {
  823.  
  824.   if (c < 040 || (sevenbit_strings && c >= 0177)) {
  825.     switch (c)
  826.       {
  827.       case '\n':
  828.     fputs_filtered ("\\n", stream);
  829.     break;
  830.       case '\b':
  831.     fputs_filtered ("\\b", stream);
  832.     break;
  833.       case '\t':
  834.     fputs_filtered ("\\t", stream);
  835.     break;
  836.       case '\f':
  837.     fputs_filtered ("\\f", stream);
  838.     break;
  839.       case '\r':
  840.     fputs_filtered ("\\r", stream);
  841.     break;
  842.       case '\033':
  843.     fputs_filtered ("\\e", stream);
  844.     break;
  845.       case '\007':
  846.     fputs_filtered ("\\a", stream);
  847.     break;
  848.       default:
  849.     fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
  850.     break;
  851.       }
  852.   } else {
  853.     if (c == '\\' || c == quoter)
  854.       fputs_filtered ("\\", stream);
  855.     fprintf_filtered (stream, "%c", c);
  856.   }
  857. }
  858.  
  859. /* Number of lines per page or UINT_MAX if paging is disabled.  */
  860. static unsigned int lines_per_page;
  861. /* Number of chars per line or UNIT_MAX is line folding is disabled.  */
  862. static unsigned int chars_per_line;
  863. /* Current count of lines printed on this page, chars on this line.  */
  864. static unsigned int lines_printed, chars_printed;
  865.  
  866. /* Buffer and start column of buffered text, for doing smarter word-
  867.    wrapping.  When someone calls wrap_here(), we start buffering output
  868.    that comes through fputs_filtered().  If we see a newline, we just
  869.    spit it out and forget about the wrap_here().  If we see another
  870.    wrap_here(), we spit it out and remember the newer one.  If we see
  871.    the end of the line, we spit out a newline, the indent, and then
  872.    the buffered output.
  873.  
  874.    wrap_column is the column number on the screen where wrap_buffer begins.
  875.      When wrap_column is zero, wrapping is not in effect.
  876.    wrap_buffer is malloc'd with chars_per_line+2 bytes. 
  877.      When wrap_buffer[0] is null, the buffer is empty.
  878.    wrap_pointer points into it at the next character to fill.
  879.    wrap_indent is the string that should be used as indentation if the
  880.      wrap occurs.  */
  881.  
  882. static char *wrap_buffer, *wrap_pointer, *wrap_indent;
  883. static int wrap_column;
  884.  
  885. /* ARGSUSED */
  886. static void 
  887. set_width_command (args, from_tty, c)
  888.      char *args;
  889.      int from_tty;
  890.      struct cmd_list_element *c;
  891. {
  892.   if (!wrap_buffer)
  893.     {
  894.       wrap_buffer = (char *) xmalloc (chars_per_line + 2);
  895.       wrap_buffer[0] = '\0';
  896.     }
  897.   else
  898.     wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
  899.   wrap_pointer = wrap_buffer;    /* Start it at the beginning */
  900. }
  901.  
  902. static void
  903. prompt_for_continue ()
  904. {
  905.   char *ignore;
  906.  
  907.   immediate_quit++;
  908.   ignore = gdb_readline ("---Type <return> to continue---");
  909.   if (ignore)
  910.     free (ignore);
  911.   chars_printed = lines_printed = 0;
  912.   immediate_quit--;
  913.   dont_repeat ();        /* Forget prev cmd -- CR won't repeat it. */
  914. }
  915.  
  916. /* Reinitialize filter; ie. tell it to reset to original values.  */
  917.  
  918. void
  919. reinitialize_more_filter ()
  920. {
  921.   lines_printed = 0;
  922.   chars_printed = 0;
  923. }
  924.  
  925. /* Indicate that if the next sequence of characters overflows the line,
  926.    a newline should be inserted here rather than when it hits the end. 
  927.    If INDENT is nonzero, it is a string to be printed to indent the
  928.    wrapped part on the next line.  INDENT must remain accessible until
  929.    the next call to wrap_here() or until a newline is printed through
  930.    fputs_filtered().
  931.  
  932.    If the line is already overfull, we immediately print a newline and
  933.    the indentation, and disable further wrapping.
  934.  
  935.    If we don't know the width of lines, but we know the page height,
  936.    we must not wrap words, but should still keep track of newlines
  937.    that were explicitly printed.
  938.  
  939.    INDENT should not contain tabs, as that
  940.    will mess up the char count on the next line.  FIXME.  */
  941.  
  942. void
  943. wrap_here(indent)
  944.   char *indent;
  945. {
  946.   if (wrap_buffer[0])
  947.     {
  948.       *wrap_pointer = '\0';
  949.       fputs (wrap_buffer, stdout);
  950.     }
  951.   wrap_pointer = wrap_buffer;
  952.   wrap_buffer[0] = '\0';
  953.   if (chars_per_line == UINT_MAX)        /* No line overflow checking */
  954.     {
  955.       wrap_column = 0;
  956.     }
  957.   else if (chars_printed >= chars_per_line)
  958.     {
  959.       puts_filtered ("\n");
  960.       puts_filtered (indent);
  961.       wrap_column = 0;
  962.     }
  963.   else
  964.     {
  965.       wrap_column = chars_printed;
  966.       wrap_indent = indent;
  967.     }
  968. }
  969.  
  970. /* Like fputs but pause after every screenful, and can wrap at points
  971.    other than the final character of a line.
  972.    Unlike fputs, fputs_filtered does not return a value.
  973.    It is OK for LINEBUFFER to be NULL, in which case just don't print
  974.    anything.
  975.  
  976.    Note that a longjmp to top level may occur in this routine
  977.    (since prompt_for_continue may do so) so this routine should not be
  978.    called when cleanups are not in place.  */
  979.  
  980. void
  981. fputs_filtered (linebuffer, stream)
  982.      const char *linebuffer;
  983.      FILE *stream;
  984. {
  985.   const char *lineptr;
  986.  
  987.   if (linebuffer == 0)
  988.     return;
  989.   
  990.   /* Don't do any filtering if it is disabled.  */
  991.   if (stream != stdout
  992.    || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
  993.     {
  994.       fputs (linebuffer, stream);
  995.       return;
  996.     }
  997.  
  998.   /* Go through and output each character.  Show line extension
  999.      when this is necessary; prompt user for new page when this is
  1000.      necessary.  */
  1001.   
  1002.   lineptr = linebuffer;
  1003.   while (*lineptr)
  1004.     {
  1005.       /* Possible new page.  */
  1006.       if (lines_printed >= lines_per_page - 1)
  1007.     prompt_for_continue ();
  1008.  
  1009.       while (*lineptr && *lineptr != '\n')
  1010.     {
  1011.       /* Print a single line.  */
  1012.       if (*lineptr == '\t')
  1013.         {
  1014.           if (wrap_column)
  1015.         *wrap_pointer++ = '\t';
  1016.           else
  1017.         putc ('\t', stream);
  1018.           /* Shifting right by 3 produces the number of tab stops
  1019.              we have already passed, and then adding one and
  1020.          shifting left 3 advances to the next tab stop.  */
  1021.           chars_printed = ((chars_printed >> 3) + 1) << 3;
  1022.           lineptr++;
  1023.         }
  1024.       else
  1025.         {
  1026.           if (wrap_column)
  1027.         *wrap_pointer++ = *lineptr;
  1028.           else
  1029.             putc (*lineptr, stream);
  1030.           chars_printed++;
  1031.           lineptr++;
  1032.         }
  1033.       
  1034.       if (chars_printed >= chars_per_line)
  1035.         {
  1036.           unsigned int save_chars = chars_printed;
  1037.  
  1038.           chars_printed = 0;
  1039.           lines_printed++;
  1040.           /* If we aren't actually wrapping, don't output newline --
  1041.          if chars_per_line is right, we probably just overflowed
  1042.          anyway; if it's wrong, let us keep going.  */
  1043.           if (wrap_column)
  1044.         putc ('\n', stream);
  1045.  
  1046.           /* Possible new page.  */
  1047.           if (lines_printed >= lines_per_page - 1)
  1048.         prompt_for_continue ();
  1049.  
  1050.           /* Now output indentation and wrapped string */
  1051.           if (wrap_column)
  1052.         {
  1053.           if (wrap_indent)
  1054.             fputs (wrap_indent, stream);
  1055.           *wrap_pointer = '\0';        /* Null-terminate saved stuff */
  1056.           fputs (wrap_buffer, stream);    /* and eject it */
  1057.           /* FIXME, this strlen is what prevents wrap_indent from
  1058.              containing tabs.  However, if we recurse to print it
  1059.              and count its chars, we risk trouble if wrap_indent is
  1060.              longer than (the user settable) chars_per_line. 
  1061.              Note also that this can set chars_printed > chars_per_line
  1062.              if we are printing a long string.  */
  1063.           chars_printed = strlen (wrap_indent)
  1064.                 + (save_chars - wrap_column);
  1065.           wrap_pointer = wrap_buffer;    /* Reset buffer */
  1066.           wrap_buffer[0] = '\0';
  1067.           wrap_column = 0;        /* And disable fancy wrap */
  1068.          }
  1069.         }
  1070.     }
  1071.  
  1072.       if (*lineptr == '\n')
  1073.     {
  1074.       chars_printed = 0;
  1075.       wrap_here ((char *)0);  /* Spit out chars, cancel further wraps */
  1076.       lines_printed++;
  1077.       putc ('\n', stream);
  1078.       lineptr++;
  1079.     }
  1080.     }
  1081. }
  1082.  
  1083.  
  1084. /* fputs_demangled is a variant of fputs_filtered that
  1085.    demangles g++ names.*/
  1086.  
  1087. void
  1088. fputs_demangled (linebuffer, stream, arg_mode)
  1089.      char *linebuffer;
  1090.      FILE *stream;
  1091.      int arg_mode;
  1092. {
  1093. #define SYMBOL_MAX 1024
  1094.  
  1095. #define SYMBOL_CHAR(c) (isascii(c) \
  1096.   && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
  1097.  
  1098.   char buf[SYMBOL_MAX+1];
  1099. # define SLOP 5        /* How much room to leave in buf */
  1100.   char *p;
  1101.  
  1102.   if (linebuffer == NULL)
  1103.     return;
  1104.  
  1105.   /* If user wants to see raw output, no problem.  */
  1106.   if (!demangle) {
  1107.     fputs_filtered (linebuffer, stream);
  1108.     return;
  1109.   }
  1110.  
  1111.   p = linebuffer;
  1112.  
  1113.   while ( *p != (char) 0 ) {
  1114.     int i = 0;
  1115.  
  1116.     /* collect non-interesting characters into buf */
  1117.     while ( *p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-SLOP ) {
  1118.       buf[i++] = *p;
  1119.       p++;
  1120.     }
  1121.     if (i > 0) {
  1122.       /* output the non-interesting characters without demangling */
  1123.       buf[i] = (char) 0;
  1124.       fputs_filtered(buf, stream);
  1125.       i = 0;  /* reset buf */
  1126.     }
  1127.  
  1128.     /* and now the interesting characters */
  1129.     while (i < SYMBOL_MAX
  1130.      && *p != (char) 0
  1131.      && SYMBOL_CHAR(*p)
  1132.      && i < (int)sizeof(buf) - SLOP) {
  1133.       buf[i++] = *p;
  1134.       p++;
  1135.     }
  1136.     buf[i] = (char) 0;
  1137.     if (i > 0) {
  1138.       char * result;
  1139.       
  1140.       if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) {
  1141.     fputs_filtered(result, stream);
  1142.     free(result);
  1143.       }
  1144.       else {
  1145.     fputs_filtered(buf, stream);
  1146.       }
  1147.     }
  1148.   }
  1149. }
  1150.  
  1151. /* Print a variable number of ARGS using format FORMAT.  If this
  1152.    information is going to put the amount written (since the last call
  1153.    to INITIALIZE_MORE_FILTER or the last page break) over the page size,
  1154.    print out a pause message and do a gdb_readline to get the users
  1155.    permision to continue.
  1156.  
  1157.    Unlike fprintf, this function does not return a value.
  1158.  
  1159.    We implement three variants, vfprintf (takes a vararg list and stream),
  1160.    fprintf (takes a stream to write on), and printf (the usual).
  1161.  
  1162.    Note that this routine has a restriction that the length of the
  1163.    final output line must be less than 255 characters *or* it must be
  1164.    less than twice the size of the format string.  This is a very
  1165.    arbitrary restriction, but it is an internal restriction, so I'll
  1166.    put it in.  This means that the %s format specifier is almost
  1167.    useless; unless the caller can GUARANTEE that the string is short
  1168.    enough, fputs_filtered should be used instead.
  1169.  
  1170.    Note also that a longjmp to top level may occur in this routine
  1171.    (since prompt_for_continue may do so) so this routine should not be
  1172.    called when cleanups are not in place.  */
  1173.  
  1174. static void
  1175. vfprintf_filtered (stream, format, args)
  1176.      FILE *stream;
  1177.      char *format;
  1178.      va_list args;
  1179. {
  1180.   static char *linebuffer = (char *) 0;
  1181.   static int line_size;
  1182.   int format_length;
  1183.  
  1184.   format_length = strlen (format);
  1185.  
  1186.   /* Allocated linebuffer for the first time.  */
  1187.   if (!linebuffer)
  1188.     {
  1189.       linebuffer = (char *) xmalloc (255);
  1190.       line_size = 255;
  1191.     }
  1192.  
  1193.   /* Reallocate buffer to a larger size if this is necessary.  */
  1194.   if (format_length * 2 > line_size)
  1195.     {
  1196.       line_size = format_length * 2;
  1197.  
  1198.       /* You don't have to copy.  */
  1199.       free (linebuffer);
  1200.       linebuffer = (char *) xmalloc (line_size);
  1201.     }
  1202.  
  1203.  
  1204.   /* This won't blow up if the restrictions described above are
  1205.      followed.   */
  1206.   (void) vsprintf (linebuffer, format, args);
  1207.  
  1208.   fputs_filtered (linebuffer, stream);
  1209. }
  1210.  
  1211. /* VARARGS */
  1212. void
  1213. fprintf_filtered (va_alist)
  1214.      va_dcl
  1215. {
  1216.   FILE *stream;
  1217.   char *format;
  1218.   va_list args;
  1219.  
  1220.   va_start (args);
  1221.   stream = va_arg (args, FILE *);
  1222.   format = va_arg (args, char *);
  1223.  
  1224.   /* This won't blow up if the restrictions described above are
  1225.      followed.   */
  1226.   vfprintf_filtered (stream, format, args);
  1227.   va_end (args);
  1228. }
  1229.  
  1230. /* VARARGS */
  1231. void
  1232. printf_filtered (va_alist)
  1233.      va_dcl
  1234. {
  1235.   va_list args;
  1236.   char *format;
  1237.  
  1238.   va_start (args);
  1239.   format = va_arg (args, char *);
  1240.  
  1241.   vfprintf_filtered (stdout, format, args);
  1242.   va_end (args);
  1243. }
  1244.  
  1245. /* Easy */
  1246.  
  1247. void
  1248. puts_filtered (string)
  1249.      char *string;
  1250. {
  1251.   fputs_filtered (string, stdout);
  1252. }
  1253.  
  1254. /* Return a pointer to N spaces and a null.  The pointer is good
  1255.    until the next call to here.  */
  1256. char *
  1257. n_spaces (n)
  1258.      int n;
  1259. {
  1260.   register char *t;
  1261.   static char *spaces;
  1262.   static int max_spaces;
  1263.  
  1264.   if (n > max_spaces)
  1265.     {
  1266.       if (spaces)
  1267.     free (spaces);
  1268.       spaces = (char *) xmalloc (n+1);
  1269.       for (t = spaces+n; t != spaces;)
  1270.     *--t = ' ';
  1271.       spaces[n] = '\0';
  1272.       max_spaces = n;
  1273.     }
  1274.  
  1275.   return spaces + max_spaces - n;
  1276. }
  1277.  
  1278. /* Print N spaces.  */
  1279. void
  1280. print_spaces_filtered (n, stream)
  1281.      int n;
  1282.      FILE *stream;
  1283. {
  1284.   fputs_filtered (n_spaces (n), stream);
  1285. }
  1286.  
  1287. /* C++ demangler stuff.  */
  1288.  
  1289. /* Print NAME on STREAM, demangling if necessary.  */
  1290. void
  1291. fprint_symbol (stream, name)
  1292.      FILE *stream;
  1293.      char *name;
  1294. {
  1295.   char *demangled;
  1296.   if ((!demangle) || NULL == (demangled = cplus_demangle (name, 1)))
  1297.     fputs_filtered (name, stream);
  1298.   else
  1299.     {
  1300.       fputs_filtered (demangled, stream);
  1301.       free (demangled);
  1302.     }
  1303. }
  1304.  
  1305. void
  1306. _initialize_utils ()
  1307. {
  1308.   struct cmd_list_element *c;
  1309.  
  1310.   c = add_set_cmd ("width", class_support, var_uinteger, 
  1311.           (char *)&chars_per_line,
  1312.           "Set number of characters gdb thinks are in a line.",
  1313.           &setlist);
  1314.   add_show_from_set (c, &showlist);
  1315.   c->function.sfunc = set_width_command;
  1316.  
  1317.   add_show_from_set
  1318.     (add_set_cmd ("height", class_support,
  1319.           var_uinteger, (char *)&lines_per_page,
  1320.           "Set number of lines gdb thinks are in a page.", &setlist),
  1321.      &showlist);
  1322.   
  1323.   /* These defaults will be used if we are unable to get the correct
  1324.      values from termcap.  */
  1325.   lines_per_page = 24;
  1326.   chars_per_line = 80;
  1327. #ifdef sprite  
  1328.   isatty_stdout = ISATTY(stdout);
  1329. #endif  
  1330.   /* Initialize the screen height and width from termcap.  */
  1331.   {
  1332.     char *termtype = getenv ("TERM");
  1333.  
  1334.     /* Positive means success, nonpositive means failure.  */
  1335.     int status;
  1336.  
  1337.     /* 2048 is large enough for all known terminals, according to the
  1338.        GNU termcap manual.  */
  1339.     char term_buffer[2048];
  1340.  
  1341.     if (termtype)
  1342.       {
  1343.     status = tgetent (term_buffer, termtype);
  1344.     if (status > 0)
  1345.       {
  1346.         int val;
  1347.         
  1348.         val = tgetnum ("li");
  1349.         if (val >= 0)
  1350.           lines_per_page = val;
  1351.         else
  1352.           /* The number of lines per page is not mentioned
  1353.          in the terminal description.  This probably means
  1354.          that paging is not useful (e.g. emacs shell window),
  1355.          so disable paging.  */
  1356.           lines_per_page = UINT_MAX;
  1357.         
  1358.         val = tgetnum ("co");
  1359.         if (val >= 0)
  1360.           chars_per_line = val;
  1361.       }
  1362.       }
  1363.   }
  1364.  
  1365. #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
  1366.  
  1367.   /* If there is a better way to determine the window size, use it. */
  1368.   SIGWINCH_HANDLER ();
  1369. #endif
  1370.  
  1371.   /* If the output is not a terminal, don't paginate it.  */
  1372.   if (!ISATTY (stdout))
  1373.     lines_per_page = UINT_MAX;
  1374.  
  1375.   set_width_command ((char *)NULL, 0, c);
  1376.  
  1377.   add_show_from_set
  1378.     (add_set_cmd ("demangle", class_support, var_boolean, 
  1379.           (char *)&demangle,
  1380.         "Set demangling of encoded C++ names when displaying symbols.",
  1381.           &setprintlist),
  1382.      &showprintlist);
  1383.  
  1384.   add_show_from_set
  1385.     (add_set_cmd ("sevenbit-strings", class_support, var_boolean, 
  1386.           (char *)&sevenbit_strings,
  1387.    "Set printing of 8-bit characters in strings as \\nnn.",
  1388.           &setprintlist),
  1389.      &showprintlist);
  1390.  
  1391.   add_show_from_set
  1392.     (add_set_cmd ("asm-demangle", class_support, var_boolean, 
  1393.           (char *)&asm_demangle,
  1394.     "Set demangling of C++ names in disassembly listings.",
  1395.           &setprintlist),
  1396.      &showprintlist);
  1397. }
  1398.  
  1399. /* Machine specific function to handle SIGWINCH signal. */
  1400.  
  1401. #ifdef  SIGWINCH_HANDLER_BODY
  1402.         SIGWINCH_HANDLER_BODY
  1403. #endif
  1404. @
  1405.